<?php

$sql = <<<EOT
DROP TABLE IF EXISTS `paylog`;
CREATE TABLE `paylog` (
  `Id` int(11) NOT NULL auto_increment COMMENT '自增id',
  `uid` int(11) NOT NULL COMMENT '用户id',
  `pay` int(11) NOT NULL COMMENT '充值钱数量',
  `channel` varchar(50) NOT NULL COMMENT '渠道',
  `GameCash` int(11) NOT NULL COMMENT '对应游戏里面的钱币',
  `BeforeCash` int(11) default NULL COMMENT '支付前用的点券数',
  `AlreadyPay` tinyint(4) default '0' COMMENT '是否已经真实支付\n1:已经支付\n0:还没有',
  `IP` int(10) unsigned default NULL COMMENT '客户端ip地址',
  `CreateTime` datetime default NULL COMMENT '创建账单时间',
  `UpdateTime` datetime default NULL COMMENT '更新账单时间',
  `PlatFormOrderId` varchar(128) default NULL COMMENT '平台订单Id',
  PRIMARY KEY  (`Id`),
  KEY  (`uid`),
  KEY  (`PlatFormOrderId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='支付日志表';
EOT;

// 获取表名
preg_match_all("/CREATE TABLE `(.*?)` \(/", $sql, $matches);
$tableName = $matches[1][0];

// 获取主键
preg_match_all("/PRIMARY KEY\s+\(`(.*?)`\)/", $sql, $matches);
$primary = $matches[1][0];

// 获取字段列表
preg_match_all("/^\s+`(.*?)`\s+(\w+).*?,/m", $sql, $matches);
$arr_keys = array();
foreach ($matches[1] as $index => $v){
	$arr_keys[$v] = array('key' => $v, 'type' => $matches[2][$index]);
}

crateModel($tableName, $primary, $arr_keys);


/**
 * 创建model
 *
 * @param unknown_type $tableName
 * @param unknown_type $arr_keys
 */
function crateModel($tableName, $primary, $arr_keys)
{
	$modelName = ucfirst(strtolower($tableName)) . 'Model';
	$insertStr = '';
	$changeStr = '';
	foreach ($arr_keys as $index => $keys){
		if ($keys['key'] == $primary)
			$insertStr .= '//';
		
		$typePre = '';
		if (strpos($keys['type'], 'int') !== false)
			$typePre = '(int)';
		
		$insertStr .= '		$v["' . $keys['key'] . '"] = ' . $typePre . '$value["' . $keys['key'] .'"];' . "\r\n";
		
		if ($keys['key'] != $primary)
			$changeStr .= '		isset($value["' . $keys['key'] . '"]) && $v["' . $keys['key'] . '"] = ' . $typePre . '$value["' . $keys['key'] . '"];' . "\r\n";
	}
	
	
	$template = <<<EOT
<?php

/**
 * {$modelName}.php 注释
 * 
 * @package 
 * @subpackage 
 * 
 */
class {$modelName}{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private \$db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql \$db
	 */
	public function __construct(\$db)
	{
		\$this->db = \$db;
	}
	
	/**
	 * 添加
	 */
	public function add( array \$value )
	{
		\$v = array();
{$insertStr}		
		\$this->db->insert("{$tableName}", \$v);
	}
	
	/**
	 * 修改
	 */
	public function change(\${$primary}, array \$value)
	{
		\$v = array();
{$changeStr}
		\$this->db->update("{$tableName}", \$v, '{$primary}=' . (int)\${$primary});
	}
	
	/**
	 * 删除
	 */
	public function del(\${$primary})
	{
		\$this->db->delete("{$tableName}", '{$primary}=' .  (int)\${$primary});
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer \$nowp
	 * @param Integer \$per_page
	 */
	public function fetchAll(\$nowp, \$per_page)
	{
		\$where = '';		
		\$totalNum = \$this->db->fetchOne ('select count(*) from {$tableName} ' . \$where);
        
        require_once APP . '/application/models/Util.php';
        
        \$pageInfo = Util::page (\$totalNum, \$per_page, \$nowp);
        \$start    = \$pageInfo ['start'];
        \$list     = \$this->db->fetchAll ('select * from {$tableName}' . \$where .' order by {$primary} desc limit ' . \$start . ',' . \$per_page);
        
        return Array ('page' => \$pageInfo['page'], 'list' => \$list);
	}
	
	/**
	 * 显示
	 */
	public function show(\${$primary})
	{
		return \$this->db->fetchRow("select * from `{$tableName}` where {$primary}=\"?\"", array(\${$primary}));
	}
}
EOT;

	file_put_contents($modelName . '.php', $template);
}